package cz.muni.fi.heathtow.web.pages;

import cz.muni.fi.heathrowapi.HeathrowRoles;
import cz.muni.fi.heathrowapi.UserService;
import cz.muni.fi.heathrowapi.dto.UserDTO;
import java.util.AbstractSet;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.apache.wicket.authroles.authentication.AuthenticatedWebSession;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.markup.html.form.StatelessForm;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.form.validation.EqualPasswordInputValidator;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;

/**
 *
 * @author Jan Richter
 */
public class SignIn extends MainPage {

    public SignIn(PageParameters parameters) {
        super(parameters);
        final LoginForm form = new LoginForm("loginForm");
        add(form);
    }

    private static class LoginForm extends StatelessForm {

        private static final long serialVersionUID = -6826853507535977683L;
        private String username;
        private String password;

        public LoginForm(String id) {
            super(id);
            setModel(new CompoundPropertyModel(this));
            add(new Label("usernameLabel", new StringResourceModel("username.label", this, null)));
            RequiredTextField userName = new RequiredTextField("username");
            userName.setLabel(new ResourceModel("username.label"));
            add(userName);

            add(new Label("passwordLabel", new StringResourceModel("password.label", this, null)));
            PasswordTextField password = new PasswordTextField("password");
            password.setLabel(new ResourceModel("password.label"));
            add(password);
            add(new FeedbackPanel("feedback"));

        }

        @Override
        protected void onSubmit() {
            AuthenticatedWebSession session = AuthenticatedWebSession.get();
            if (session.signIn(username, password)) {
                setResponsePage(HomePage.class);
            } else {
                error(getString("login.failed.badcredentials"));
            }
        }
    }
}
